<template>
  <view class="device-page">
    <!-- 状态筛选 -->
    <view class="filter-container">
      <view 
        v-for="filter in filters"
        :key="filter.value"
        :class="['filter-item', { active: activeFilter === filter.value }]"
        @click="changeFilter(filter.value)"
      >
        {{ filter.label }}
      </view>
    </view>

    <!-- 设备列表 -->
    <scroll-view 
      class="device-list" 
      scroll-y
      refresher-enabled
      @refresherrefresh="refreshData"
      @scrolltolower="loadMore"
    >
      <!-- 设备卡片 -->
      <view 
        v-for="device in filteredDeviceList"
        :key="device.id"
        class="device-card"
        @click="navigateToDetail(device.id)"
      >
        <image 
          :src="device.thumbnail" 
          class="device-thumb"
          mode="aspectFill"
        />
        <view class="card-content">
          <view 
            class="status-tag" 
            :class="device.status"
            :style="getStatusStyle(device.status)"
          >
            {{ statusLabels[device.status] }}
          </view>
          <text class="device-name">{{ device.name }}</text>
          <view class="meta-info">
            <uni-icons type="location" size="14" color="#666" />
            <text class="area">{{ device.area || '未设置区域' }}</text>
          </view>
          <view class="progress-wrapper">
            <view class="progress-bar" :style="{ width: `${device.battery}%` }"></view>
            <text class="battery">{{ device.battery }}% 剩余电量</text>
          </view>
          <text class="update-time">{{ formatTime(device.lastActive) }}</text>
        </view>
      </view>

      <uni-load-more :status="loadStatus" />
    </scroll-view>

    <!-- 悬浮添加按钮 -->
    <view class="fab" @click="navigateToAdd">
      <uni-icons type="plus" size="28" color="#fff" />
    </view>
  </view>
</template>

<script setup>
import { ref, computed } from 'vue'

// 设备数据（包含三种状态）
const originalDeviceList = ref([
  {
    id: 1,
    name: '扫地机器人S1',
    status: 'online',       // 在线
    battery: 85,
    thumbnail: 'https://picsum.photos/300/200?random=1',
    lastActive: Date.now() - 3600000, // 1小时前
    area: '客厅'
  },
  {
    id: 2,
    name: '扫地机器人S2',
    status: 'offline',      // 离线（超过12小时未活动）
    battery: 60,
    thumbnail: 'https://picsum.photos/300/200?random=2',
    lastActive: Date.now() - 43200000, // 12小时前
    area: '卧室'
  },
  {
    id: 3,
    name: '扫地机器人S3',
    status: 'maintain',     // 维护中
    battery: 30,
    thumbnail: 'https://picsum.photos/300/200?random=3',
    lastActive: Date.now() - 10800000, // 3小时前
    area: '书房'
  },
  {
    id: 4,
    name: '扫地机器人S4',
    status: 'online',       // 在线
    battery: 90,
    thumbnail: 'https://picsum.photos/300/200?random=4',
    lastActive: Date.now() - 1800000, // 30分钟前
    area: '厨房'
  }
])

// 响应式筛选
const activeFilter = ref('all')
const filteredDeviceList = computed(() => {
  if (activeFilter.value === 'all') return originalDeviceList.value
  return originalDeviceList.value.filter(device => device.status === activeFilter.value)
})

// 状态配置
const filters = [
  { label: '全部', value: 'all' },
  { label: '在线', value: 'online' },
  { label: '离线', value: 'offline' },
  { label: '维护中', value: 'maintain' }
]

const statusLabels = {
  online: '在线',
  offline: '离线',
  maintain: '维护中'
}

// 状态样式
const getStatusStyle = (status) => {
  return {
    online: { background: '#4CD964', boxShadow: '0 4rpx 12rpx rgba(76,217,100,0.3)' },
    offline: { background: '#999', boxShadow: '0 4rpx 12rpx rgba(153,153,153,0.3)' },
    maintain: { background: '#FF9500', boxShadow: '0 4rpx 12rpx rgba(255,149,0,0.3)' }
  }[status] || {}
}

// 交互方法
const changeFilter = (value) => {
  activeFilter.value = value
}

const navigateToDetail = (id) => {
  uni.navigateTo({ url: `/pages/device/control/control` })
  // console.log('查看设备详情', id)
}

const navigateToAdd = () => {
  uni.navigateTo({ url: '/pages/device/add' })
  console.log('添加新设备')
}

// 时间格式化
const formatTime = (timestamp) => {
  const date = new Date(timestamp)
  return `${date.getHours()}:${date.getMinutes().toString().padStart(2, '0')}`
}
</script>

<style>
.device-page {
  background: #f8f9fc;
  min-height: 100vh;
}

.filter-container {
  display: flex;
  padding: 24rpx 28rpx;
  background: #fff;
  box-shadow: 0 4rpx 12rpx rgba(0,0,0,0.06);
}

.filter-item {
  flex: 1;
  margin: 0 12rpx;
  padding: 12rpx 0;
  text-align: center;
  border-radius: 32rpx;
  background: #f5f6fa;
  color: #666;
  font-size: 28rpx;
  transition: all 0.3s;
}

.filter-item.active {
  background: #6B73FF;
  color: #fff;
  box-shadow: 0 4rpx 12rpx rgba(107,115,255,0.3);
}

.device-list {
  box-sizing: border-box;
  height: calc(100vh - 100rpx);
  padding: 0 24rpx;
}

.device-card {
  background: #fff;
  border-radius: 24rpx;
  margin-bottom: 24rpx;
  box-shadow: 0 8rpx 24rpx rgba(0,0,0,0.06);
  overflow: hidden;
}

.device-thumb {
  width: 100%;
  height: 320rpx;
  background: #f5f6fa;
}

.card-content {
  padding: 24rpx;
  position: relative;
}

.status-tag {
  position: absolute;
  top: -36rpx;
  right: 24rpx;
  padding: 8rpx 24rpx;
  border-radius: 32rpx;
  color: #fff;
  font-size: 24rpx;
  white-space: nowrap;
}

.device-name {
  display: block;
  font-size: 32rpx;
  font-weight: 500;
  margin-bottom: 16rpx;
}

.meta-info {
  display: flex;
  align-items: center;
  margin-bottom: 24rpx;
}

.area {
  font-size: 26rpx;
  color: #666;
  margin-left: 12rpx;
}

.progress-wrapper {
  height: 48rpx;
  background: #f5f6fa;
  border-radius: 24rpx;
  margin-bottom: 24rpx;
  position: relative;
}

.progress-bar {
  height: 100%;
  background: linear-gradient(90deg, #6B73FF, #409EFF);
  border-radius: 24rpx;
  transition: width 0.3s;
}

.battery {
  position: absolute;
  right: 24rpx;
  top: 50%;
  transform: translateY(-50%);
  color: #fff;
  font-size: 24rpx;
}

.update-time {
  display: block;
  color: #999;
  font-size: 24rpx;
  text-align: right;
}

.fab {
  position: fixed;
  right: 40rpx;
  bottom: 150rpx;
  width: 96rpx;
  height: 96rpx;
  border-radius: 50%;
  background: linear-gradient(135deg, #6B73FF, #409EFF);
  box-shadow: 0 12rpx 32rpx rgba(107,115,255,0.3);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 10;
  transition: transform 0.2s;
}

.fab:active {
  transform: scale(0.9);
}
</style>